package com.wh.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wh.springboot.common.entity.Menu;
import com.wh.springboot.mapper.MenuMapper;
import com.wh.springboot.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author elsy
 * @since 2023-03-09
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<Menu> findMenus(String title) {

        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        //①
//        if (StrUtil.isNotBlank(title)) {
//            queryWrapper.like("title", title);
//
//        }
//        queryWrapper.orderByDesc("id");
        //②
        if (!"".equals(title)) {
            queryWrapper.like("title", title);
        }
        //查找所有
        List<Menu> lists = list(queryWrapper);
        //找到 pid=null 的一级菜单
        List<Menu> parentNodes = lists.stream().filter(menu -> menu.getPid() == null).collect(Collectors.toList());
        //一级菜单子菜单
        for (Menu menu : parentNodes) {
            //筛选所有数据中pid=父级id的数据
            menu.setChildren(lists.stream().filter(m -> menu.getId().equals(m.getPid())).collect(Collectors.toList()));
        }
        return parentNodes;
    }
}
